﻿var lastidElementClick = null;

$(document).ready(function()
{	
	$(window).resize(function()
	{
      	riposizioneBoxInvitaAmico();
    });
	
	$(window).scroll(function()
	{
      	riposizioneBoxInvitaAmico();
    });
	
	jQuery.validator.addMethod("notEqualTo", function(value, element, param)
	{
	   return this.optional(element) || value != $(param).val();
	}, "This has to be different...");	
		
	$("#invitaAmico").validate(
	{		
		
		focusInvalid:false,
		invalidHandler: function(form, validator) 
		{
							
		},			
				
		onfocusout: function(element, event) {			
			
		},
		onkeyup: function(element, event) {			
			
		},
		onclick: function(element, event) {			
			
		},				
		errorPlacement: function(error, element) 
		{					
			var title = Joomla.JText._('TPL_PANERAI_LABEL_ATTENZIONE');
			var messaggioErrore = $(element).attr('errorText');
			AlertShow(title,messaggioErrore);
		},
		submitHandler: function()
		{				
			onInvitaAmico();
			return false;		 	
		},					
		rules:
		{	
			terminiInvita:
			{
				required:true
			},
			yourname:
			{
				notEqualTo:'#default_tuo_nome',
				required:true
			},			
			friendname:
			{
				required:true,
				notEqualTo:'#default_suo_nome'
			},
			friendmail:
			{
				required:true,
				email: true
			}				
		}
	});		
});

function onInvitaAmico()
{	
	/*
	var response = Recaptcha.get_response();
	var challenge = Recaptcha.get_challenge();
	
	var requestUrl = base_url+'templates/panerai/html/checkCaptcha.php?recaptcha_response_field='+response+'&recaptcha_challenge_field='+challenge;
	
	$.ajax({
	type: "POST",
	url: requestUrl,
	data: {recaptcha_response_field : response, recaptcha_challenge_field: challenge},
	success: function(data) 
	{
	   stato = $(data).find("root").find('status').text();	
	   
		if(stato == 1)
		{
			onServerRequest('invitaAmico',onResponseInvite);		
		}
		if(stato == -2)
		{
			var msg = 'ERRORE';
			//var titolo = Joomla.JText._('TPL_CATALOGO_FORM_BENVENUTO'); 
			//var titolo = Joomla.JText._('ERRORE'); 
			var titolo = "CODE VALIDATION ERROR"; 
			$("#btn-submit-form").css('display','inline');
			AlertShow(titolo,msg);
		}
		else
		{
			var msg = 'ERRORE';
			//var titolo = Joomla.JText._('TPL_CATALOGO_FORM_BENVENUTO'); 
			//var titolo = Joomla.JText._('ERRORE'); 
			var titolo = "CODE VALIDATION ERROR"; 
			$("#btn-submit-form").css('display','inline');
			AlertShow(titolo,msg);
		}
	   
		
	},
	error: function() 
	{
		var msg = 'ERRORE';//$(this).find('message').text(); //Joomla.JText._($(this).find('message').text());
		var titolo = "CODE VALIDATION ERROR"; 
		AlertShow(titolo,msg);
	}
	
	});
	*/
	onServerRequest('invitaAmico',onResponseInvite);		
	
}

	
function onResponseInvite(xml)
{	
	var status = -1;	  	   
	$('#box_invita_amico').fadeOut(500);
	hideSocialOverProduct();
	resetFormInvita();
  
  $(xml).find("root").each(function()
   {
		$(this).find('status').each(function()
		{
			status = $(this).text();
		});		
		
		if(status == 1)
		{
			var title = Joomla.JText._('TPL_PANERAI_LABEL_COMPLIMENTI');
			var messaggio=  Joomla.JText._('TPL_PANERAI_LABEL_INVITA_AMICO_MAIL_OK');
			AlertShow(title,messaggio,trackEventGTMSendInvitaAmicoOK);
			
		}
		else if(status == -2)
		{
			var titolo = "CODE VALIDATION ERROR"; 
			var msg = 'ERROR';
			$("#btn-submit-form").css('display','inline');
			AlertShow(titolo,msg);
		}else
		{
			var title = Joomla.JText._('TPL_PANERAI_LABEL_ATTENZIONE');
			var messaggio=  Joomla.JText._('TPL_PANERAI_LABEL_INVITA_AMICO_MAIL_NOK');
			AlertShow(title,messaggio);
		}				
   });
}

function trackEventGTMSendInvitaAmicoOK()
{
	dataLayer.push({"event":"emailShare2"});
}

function showHideBoxInvitaAmcio(pVal,idElementClick)
{	
	resetFormInvita();
	if(pVal == true)
	{
		dataLayer.push({"event":"emailShare1"});
		lastidElementClick = idElementClick;
		left = $('#'+idElementClick).offset().left - 260;
		margintop = $('#'+idElementClick).offset().top -450; 

		//if(timeOutOverSocial != undefined && timeOutOverSocial != "")
		//{
			clearTimeout(timeOutOverSocial);
			timeOutOverSocial = "";
		//}

		$('#box_invita_amico').css('left',left+'px');
		$('#box_invita_amico').css('top',margintop+'px');
		
		$('#box_invita_amico').fadeIn(300);
		
		//$("#captcha_invita_amico").html('');
		/*
		var captcha = '<script type="text/javascript">var RecaptchaOptions = {tabindex: 1,theme: \'custom\',custom_theme_widget: \'recaptcha_widget\'};</script><div id="recaptcha_widget" style="display:none"><div id="recaptcha_image"></div><div class="recaptcha_only_if_incorrect_sol" style="color:red">Sbagliato, riprova</div><span class="recaptcha_only_if_image">Inserisci le parole sopra:</span><span class="recaptcha_only_if_audio">Digita ci� che senti:</span><input type="text" id="recaptcha_response_field" name="recaptcha_response_field"><div><a href="javascript:Recaptcha.reload()">Visualizza un altro CAPTCHA</a></div><div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(&#39;audio&#39;)">Ottieni un CAPTCHA audio</a></div><div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(&#39;image&#39;)">Visualizza unimmagine CAPTCHA</a></div><div><a href="javascript:Recaptcha.showhelp()">Guida</a></div><script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LeoYeASAAAAAEc83fSoWMA2rrHmtrBFKD84eGvC"></script><noscript><iframe src="http://www.google.com/recaptcha/api/noscript?k=6LeoYeASAAAAAEc83fSoWMA2rrHmtrBFKD84eGvC" height="300" width="500" frameborder="0"></iframe><br><textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea><input type="hidden" name="recaptcha_response_field" value="manual_challenge"></noscript></div><script type="text/javascript"> window.onload = function() {Recaptcha.focus_response_field();}</script>';
		
		$("#captcha_invita_amico").html(captcha);
		*/
		/*
		var RecaptchaOptions = {tabindex: 1,theme: 'custom',custom_theme_widget: 'recaptcha_widget'};
		
		*/

	}else
	{
		$('#box_invita_amico').fadeOut(200);
		hideSocialOverProduct();
	}	
}

function resetFormInvita()
{
	var defaultText = $("#yourname").attr("defaultText");
	$('#yourname').attr('value',defaultText);
	defaultText = $("#friendname").attr("defaultText");
	$('#friendname').attr('value',defaultText);
	defaultText = $("#friendmail").attr("defaultText");
	$('#friendmail').attr('value',defaultText);	
}

function riposizioneBoxInvitaAmico()
{
	if($('#box_invita_amico').css('display') != 'none' && lastidElementClick != null)
	{	
		left = $('#'+lastidElementClick).offset().left - 260;
		margintop = $('#'+lastidElementClick).offset().top - 450; 
	
		$('#box_invita_amico').css('left',left+'px');
		$('#box_invita_amico').css('top',margintop+'px');
	}			
}

function resetText(id)
{
	var defaultText = $("#"+id).attr("defaultText");
		
	var value = $('#'+id).attr('value');
	
	if(value == "")
	{		
		$('#'+id).attr('value',defaultText)
	}
	
	if(value == defaultText)
	{
		$('#'+id).attr('value','');
	}	
}

function sendMailInvitaAmico()
{		
	$("#invitaAmico").submit();
	resetFormInvita();	
}

$(function () {
    $(".social_prod a.fb, .sub_wrap_social_hover a.fb").click(function (e) 
	{
        e.preventDefault();
        var url = "";
		var id = jQuery.trim($(this).attr("data"));
		var typeObj= $(this).attr("typeObj");
		var urlToShare =  $(this).attr("urlToShare");
		var language= $(this).attr("language");	
		var d = new Date();
		var noCache = d.getTime(); 
		if($(this).attr("virtualUrl") == 1)//devo ricostruire la pagina
			url = jQuery.trim(base_url + 'templates/panerai/html/share.php?myid='+id+'&mytypeObj='+typeObj+'&mylanguage='+language+'&nc='+noCache+'&myurlToShare='+urlToShare);//?urlToShare='+$.base64Encode(urlToShare)+'&typeObj='+typeObj+'&language='+language+'&id='+id);
		else
			url = $(this).attr("data");//condivisione diretta		
		
        window.open("https://www.facebook.com/sharer.php?u=" +encodeURIComponent(jQuery.trim(url)), "share", "height=440,width=620,scrollbars=true");
    });
    $(".social_prod a.weibo, .sub_wrap_social_hover a.weibo").click(function (e) 
	{
        e.preventDefault();
        var url = "";
		var id = jQuery.trim($(this).attr("data"));
		var typeObj= $(this).attr("typeObj");
		var urlToShare =  $(this).attr("urlToShare");
		var language= $(this).attr("language");	
		var d = new Date();
		var noCache = d.getTime(); 
		if($(this).attr("virtualUrl") == 1)//devo ricostruire la pagina
			url = jQuery.trim(base_url + 'templates/panerai/html/share.php?myid='+id+'&mytypeObj='+typeObj+'&mylanguage='+language+'&nc='+noCache+'&myurlToShare='+urlToShare);//?urlToShare='+$.base64Encode(urlToShare)+'&typeObj='+typeObj+'&language='+language+'&id='+id);
		else
			url = $(this).attr("data");//condivisione diretta		
		          //   http://service.weibo.com/share/share.php?url=http://example.com&appkey=&title=Description&pic=&ralateUid=&language=zh_cn  %2F
        
		//window.open("http://service.weibo.com/share/share.php?url="+encodeURIComponent(jQuery.trim(url))+"&title="+id , "share", "height=440,width=620,scrollbars=true");
        
        //window.open("http://v.t.sina.com.cn/share/share.php?url="+encodeURIComponent(jQuery.trim(url))+"&title="+id, "share", "height=440,width=620,scrollbars=true");

        window.open("http://service.weibo.com/share/share.php?url=" +encodeURIComponent(jQuery.trim(url))+"&appkey=&title="+id+"&pic=&ralateUid=&language=", "share", "height=440,width=620,scrollbars=true");
        //window.open("https://www.facebook.com/sharer.php?u=" +encodeURIComponent(jQuery.trim(url)), "share", "height=440,width=620,scrollbars=true");
    });
    $(".social_prod a.tw, .sub_wrap_social_hover a.tw").click(function (e) {
        e.preventDefault();
        var url = $(this).attr("data");
        var text = $(this).attr("testo");        
		
		window.open('https://twitter.com/share?url='+url+'&text='+text+'', 'shareTwitter', 'toolbar=0,location=0, directories=0, status=0, menubar=0,scrollbars=1,resizable=0,width=550,height=300');return false;
    });

    $(".social_prod a.gp-share, .sub_wrap_social_hover a.gp-share").click(function (e) {
       /* e.preventDefault();
		var url = $(this).attr("data");
		
		if($(this).attr("virtualUrl") == 1)
			url = base_url + 'templates/panerai/html/share.php';	      
        window.open("https://plus.google.com/share?url=" + encodeURI(url), { 'height': 650, 'width': 1024, 'scrollbars': true });*/
    });
	
    $(".social_prod a.pn, .sub_wrap_social_hover a.pn").click(function (e) {
        e.preventDefault();		
        var image = base_url + $(this).attr("image");
        var text = $(this).attr("text");
        var url = $(this).attr("data");		
		image =  $(this).attr("image");	
		
        window.open("http://pinterest.com/pin/create/button/?media=" + encodeURI(image) + "\u0026title=" + text + "\u0026description=" + text + "\u0026url=" + url);		
    }); 
	
	  $(".social_prod a.lk, .sub_wrap_social_hover a.lk").click(function (e) {
        e.preventDefault();
        var url = "";
		var titolo =$(this).attr("titolo");
		var riassunto =$(this).attr("riassunto");
		var id = jQuery.trim($(this).attr("data"));
		var typeObj= $(this).attr("typeObj");
		var urlToShare =  $(this).attr("urlToShare");
		var language= $(this).attr("language");	
		var d = new Date();
		var noCache = d.getTime(); 
		if($(this).attr("virtualUrl") == 1)//devo ricostruire la pagina
			url = jQuery.trim(base_url + 'templates/panerai/html/share.php?myid='+id+'&mytypeObj='+typeObj+'&mylanguage='+language+'&nc='+noCache+'&myurlToShare='+urlToShare);//?urlToShare='+$.base64Encode(urlToShare)+'&typeObj='+typeObj+'&language='+language+'&id='+id);
		else
			url = $(this).attr("data");//condivisione diretta		
			
        window.open("http://www.linkedin.com/shareArticle?mini=false&url="+encodeURIComponent(url));
    });     	
});